package core

import (
	"gitee.com/igolang/imoney/internal/core/client"
	coreCron "gitee.com/igolang/imoney/internal/core/cron"
	"gitee.com/igolang/imoney/internal/core/logger"
	"gitee.com/igolang/imoney/internal/models"
	"gitee.com/igolang/pkg/xlog"
)

var serves = make([]Service, 0)
var log = xlog.DefaultStdLog()

type Service interface {
	Init() error
	Shutdown() error
	Name() string
}

func init() {
	AddService(logger.Logger, client.Clients(), models.Service(), coreCron.Service())
}

func Init() {
	for _, v := range serves {
		log.Println("init service:", v.Name())
		if err := v.Init(); err != nil {
			log.Panicln("core failed to initialize err: ", err)
		}
	}
}

func Shutdown() {
	for _, v := range serves {
		if err := v.Shutdown(); err != nil {
			log.Printf("core.ShutDown err: %v\n", err)
		}
	}
}

func AddService(serv ...Service) {
	serves = append(serves, serv...)
}
